package com.epu.itepu.helper;

import java.io.IOException;

import android.app.Activity;
import android.database.Cursor;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;

public class MyExif {

	private ExifInterface exifInterface;
	private Boolean exifValid = false;
	private String exifFilePath;
	private Activity parentActivity;
	private String exifFilePath_withoutext;
	private String ext;

	public MyExif(Uri fileUri, Activity parent) {
		parentActivity = parent;
		exifFilePath = getRealPathFromURI(fileUri);
		PrepareExif();
	}

	private String getRealPathFromURI(Uri contentUri) {
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = parentActivity.managedQuery(contentUri, proj, null,
				null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	private void PrepareExif() {

		int dotposition = exifFilePath.lastIndexOf(".");
		exifFilePath_withoutext = exifFilePath.substring(0, dotposition);
		ext = exifFilePath.substring(dotposition + 1, exifFilePath.length());

		if (ext.equalsIgnoreCase("jpg")) {
			try {
				exifInterface = new ExifInterface(exifFilePath);
				exifValid = true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public ExifInterface getExifinterface() {
		if (!exifValid) {
			return null;
		} else {
			return this.exifInterface;
		}
	}
	
	public String getPath(){
		return exifFilePath_withoutext + "." + ext;
	}
	

}
